package cn.edu.xjtu.android.emoji_warfare_android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import cn.edu.xjtu.android.emoji_warfare_android.game.Config;
import cn.edu.xjtu.android.emoji_warfare_android.music.Musics;
import android.graphics.BitmapFactory;
import android.widget.ProgressBar;
import android.content.res.Resources;

/**
 * LoadingActivity<br>
 * 加载界面，主要用于加载图像资源
 * 
 * @author bill
 *
 */
public class LoadingActivity extends Activity {
    /**
     * 跳转时间
     */
    public final static long DEFAULT_REDIRECT_TIME = 0;

    public Handler mHandler;
    /**
     * 加载进度
     */
    private int process;
    /**
     * 进度条
     */
    ProgressBar pb;

    Runnable refresh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        pb = (ProgressBar) findViewById(R.id.loadingBar);
        mHandler = new Handler();
        // 用于在主线程刷新进度
        refresh = new Runnable() {
            @Override
            public void run() {
                pb.setProgress(process);
            }
        };
        // 子线程加载
        new Thread() {
            @Override
            public void run() {
                load();
            }
        }.start();
    }

    private void load() {
        // 加载图片资源
        process = 0;
        refreshProcess();
        Resources r = getResources();
        Config.Hero.setImage(0, BitmapFactory.decodeResource(r, R.drawable.hero1));
        Config.Hero.setImage(1, BitmapFactory.decodeResource(r, R.drawable.hero2));
        Config.Hero.setImage(2, BitmapFactory.decodeResource(r, R.drawable.hero3));
        Config.Hero.setImage(3, BitmapFactory.decodeResource(r, R.drawable.hero4));
        Config.Back.setBackground(BitmapFactory.decodeResource(r, R.drawable.background));
        process = 2;
        refreshProcess();
        Config.Item.setImage(0, BitmapFactory.decodeResource(r, R.drawable.item_s));
        Config.Item.setImage(1, BitmapFactory.decodeResource(r, R.drawable.item_p));
        Config.Item.setImage(2, BitmapFactory.decodeResource(r, R.drawable.item_l));
        Config.Item.setImage(3, BitmapFactory.decodeResource(r, R.drawable.item_ll));
        Config.Item.setImage(4, BitmapFactory.decodeResource(r, R.drawable.item_sh));
        process = 5;
        refreshProcess();
        Config.Bullet.setImage(0, BitmapFactory.decodeResource(r, R.drawable.bullet1));
        Config.Bullet.setImage(1, BitmapFactory.decodeResource(r, R.drawable.bullet2));
        process = 7;
        refreshProcess();
        Config.Laser.setImage(0, BitmapFactory.decodeResource(r, R.drawable.laser1));
        Config.Laser.setImage(1, BitmapFactory.decodeResource(r, R.drawable.laser2));
        Config.Laser.setImage(2, BitmapFactory.decodeResource(r, R.drawable.laser3));
        Config.Laser.setImage(3, BitmapFactory.decodeResource(r, R.drawable.laser4));
        Config.Sheild.setBaseImage(BitmapFactory.decodeResource(r, R.drawable.shield));
        process = 10;
        refreshProcess();
        for (int i = 0; i < Config.Hero.bossCount; i++) {
            // 由于raw的id递增连续 可以直接+i
            Config.Hero.setBossImage(i, BitmapFactory.decodeResource(r, R.raw.bqb_1 + i));
            process++;
            refreshProcess();
        }
        process = 20;
        refreshProcess();
        int n = Config.Sheild.lightImageCount;
        for (int i = 0; i < n; i++) {
            // 由于sheild的id递增连续 可以直接+i
            Config.Sheild.setLightImage(i, BitmapFactory.decodeResource(r, R.drawable.shield0001 + i));
            process = 20 + i * 70 / n;
            refreshProcess();
        }
        ((App) getApplication()).music = new Musics(this);
        process = 100;
        refreshProcess();
        toMain();
    }

    private void refreshProcess() {
        // 在主线程刷新
        runOnUiThread(refresh);
    }

    private void toMain() {
        // 延迟一段时间再跳转到MainActivity
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent t = new Intent(LoadingActivity.this, StartActivity.class);
                startActivity(t);
                finish();
            }
        }, DEFAULT_REDIRECT_TIME);
    }

}
